#include "BoundingBox.h"

BoundingBox::BoundingBox(void){
	height = 1.0f;
	width = 1.0f;
	depth = 1.0f;
	r = 1.0f;
	g = 1.0f;
	b = 1.0f;
}

BoundingBox::~BoundingBox(void){
}

void BoundingBox::InitBox(Vector3D position, float width, float height, float depth){
	this->position = position;
	this->height = height;
	this->width = width;
	this->depth = depth;
}

void BoundingBox::SetPosition(Vector3D position){
	this->position = position;
}

Vector3D BoundingBox::GetPosition(void){
	return position;
}

void BoundingBox::SetColor(float r, float g, float b){
	this->r = r;
	this->g = g;
	this->b = b;
}

float BoundingBox::GetHeight(void){
	return height;
}

float BoundingBox::GetWidth(void){
	return width;
}

float BoundingBox::GetDepth(void){
	return depth;
}

void BoundingBox::DrawBox(void){
	glPushMatrix();
	glTranslatef(position.x, position.y, position.z);
	glColor3f(r, g, b);
	glLineWidth(1);
	glBegin( GL_LINES );
		// Lines for the top face
		glVertex3f(0, 0, 0);
		glVertex3f(0, 0, depth);

		glVertex3f(0, 0, depth);
		glVertex3f(width, 0, depth);

		glVertex3f(width, 0, depth);
		glVertex3f(width, 0, 0);

		glVertex3f(width, 0, 0);
		glVertex3f(0, 0, 0);

		// Lines for the bottom face
		glVertex3f(0, height, 0);
		glVertex3f(0, height, depth);

		glVertex3f(0, height, depth);
		glVertex3f(width, height, depth);

		glVertex3f(width, height, depth);
		glVertex3f(width, height, 0);

		glVertex3f(width, height, 0);
		glVertex3f(0, height, 0);

		// Lines for the sides of the box
		glVertex3f(0, 0, 0);
		glVertex3f(0, height, 0);

		glVertex3f(width, 0, 0);
		glVertex3f(width, height, 0);

		glVertex3f(0, 0, depth);
		glVertex3f(0, height, depth);

		glVertex3f(width, 0, depth);
		glVertex3f(width, height, depth);
	glEnd();
	glLineWidth(1);
	glPopMatrix();
}

bool BoundingBox::HitTest(BoundingBox &box){
	float frontPtX1 = this->GetPosition().x;
	float frontPtX2 = frontPtX1 + this->GetWidth();
	float frontPtY1 = this->GetPosition().y;
	float frontPtY2 = frontPtY1 + this->GetHeight();
	float frontPtZ1 = this->GetPosition().z;
	float frontPtZ2 = frontPtZ1 + this->GetDepth();

	float frontBoxPtX1 = box.GetPosition().x;
	float frontBoxPtX2 = frontBoxPtX1 + box.GetWidth();
	float frontBoxPtY1 = box.GetPosition().y;
	float frontBoxPtY2 = frontBoxPtY1 + box.GetHeight();
	float frontBoxPtZ1 = box.GetPosition().z;
	float frontBoxPtZ2 = frontBoxPtZ1 + box.GetDepth();

	if(frontPtX2 > frontBoxPtX1 && frontPtX1 < frontBoxPtX2 
		&& frontPtY2 > frontBoxPtY1 && frontPtY1 < frontBoxPtY2 
		&& frontPtZ2 > frontBoxPtZ1 && frontPtZ1 < frontBoxPtZ2){
		this->SetColor(1.0f, 0.0f, 0.0f);
		return true;
	}
	

	/*if(backPtX2 > backBoxPtX1 && backPtX1 < backBoxPtX2 && backPtY2 > backBoxPtY1 && backPtY1 < backBoxPtY2){
		this->SetColor(0.0, 1.0, 0.0f);
		return true;
	}*/
	
	this->SetColor(1.0f, 1.0f, 1.0f);
	return false;
}